package com.kiven.beanutils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.beanutils.PropertyUtils;



public class ReflectionUtils {
	public static void main(String[] args) {
		String name = null;
		String propertyName = null;
		
		EntityTest entity = new EntityTest();
		
		PropertyDescriptor[] arrayOfPropertyDescriptors = PropertyUtils
				.getPropertyDescriptors(entity);
		for (int i=0 ; i < arrayOfPropertyDescriptors.length ; i++) {
			name = arrayOfPropertyDescriptors[i].getName();
			// 严格返回property的类型
			propertyName = arrayOfPropertyDescriptors[i].getPropertyType().getName();
			System.out.println("name = [" + name + ", type = " + propertyName +"]");
			
			
			try {
				Object localObject = entity.getClass().getMethod(
						"get" + name.substring(0, 1).toUpperCase()
								+ name.substring(1), null).invoke(entity, null);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}

		}
	}
	
	public Map setIntoMap(Object paramObject) {
		return setIntoMap(paramObject,false);
	}
	
	public Map setIntoMap(Object paramObject, boolean paramBoolean) {
		TreeMap localTreeMap1 = new TreeMap();
		TreeMap localTreeMap2 = new TreeMap();
		TreeMap localTreeMap3 = new TreeMap();
		
		PropertyDescriptor[] arrayPropertyDescriptor = PropertyUtils
				.getPropertyDescriptors(paramObject);
		for(int i=0; i<arrayPropertyDescriptor.length; i++) {
			String name = arrayPropertyDescriptor[i].getName();
			String type = arrayPropertyDescriptor[i].getPropertyType().getName();
			
			try {
				Object localObject = paramObject.getClass().getMethod(
						"get" + name.substring(0, 1).toUpperCase()
								+ name.substring(1), null).invoke(paramObject,
						null);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return null;
	}
}
